package com.example.controller;

import com.example.pojo.ConditionsQuery;
import com.example.pojo.DrugVo;
import com.example.pojo.Material;
import com.example.pojo.Result;
import com.example.service.MaterialService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/material")
public class MaterialController {
    @Autowired
    private MaterialService materialService;

    /**
     * 必备材料分页，条件查询（2025.1.8检）
     * @param conditionsQuery
     * @return
     */
    @RequestMapping("/getMaterialList")
    public Result getMaterialList(@RequestBody ConditionsQuery conditionsQuery){
        PageHelper.startPage(conditionsQuery.getPageNum(),conditionsQuery.getPageSize());

        List<Material> list=materialService.getMaterialList(conditionsQuery);
        // System.err.println(list);

        PageInfo<Material> pageInfo=new PageInfo<>(list);

        Map<String,Object> resultMap=new HashMap<>();

        //当前页数据
        resultMap.put("list",pageInfo.getList());

        //数据总数
        resultMap.put("total",pageInfo.getTotal());

        return new Result(200,"数据加载成功",resultMap);
    }

    /**
     * 必备材料添加（2025.1.8检）
     * @param material
     * @return
     */
    @RequestMapping("/add")
    public Result addMaterial(@RequestBody Material material){
        return materialService.addMaterial(material);
    }

    /**
     * 必备材料修改 （2025.1.8检）
     * @param material
     * @return
     */
    @RequestMapping("/edit")
    public Result editMaterial(@RequestBody Material material){
        return materialService.editMaterial(material);
    }

    /**
     * 必备材料单个删除（2025.1.8修正）
     * @param map
     * @return
     */
    @RequestMapping("/delMaterialById")
    public Result delMaterialById(@RequestBody Map<String,Integer> map){

        int id =map.get("id");

        return materialService.delMaterialById(id);
    }

    /**
     * 必备材料批量删除（2025.1.8检）
     * @param map
     * @return
     */
    @RequestMapping("/delBatch")
    public Result delBatch(@RequestBody Map<String,String> map){

        String ids = map.get("ids");

        return materialService.delBatch(ids);
    }
}
